package com.example.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.order.constant.OrderStatus;
import com.example.order.mapper.OrderDetailMapper;
import com.example.order.mapper.OrderInfoMapper;
import com.example.order.model.OrderDetail;
import com.example.order.model.OrderInfo;
import com.example.order.model.OrderMainVo;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName OrderServiceImpl.java
 * @Author 江南小俊
 * @Version 1.0.0
 * @Description TODO
 * @Date 2021/03/25 23:36:00
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * @param orderNo
     * @return com.example.order.model.OrderInfo
     * @description 将订单状态改为待支付并查询订单详情返回
     * @date 2021/3/26 23:58
     * @author Jim
     **/
    @Transactional
    public OrderMainVo confirmOrder(Long orderNo) {
        log.info("xid:{}", RootContext.getXID());
        OrderInfo orderInfo = getById(orderNo);
        if (orderInfo.getOrderStatus().equals(OrderStatus.CONFIRMING)) {
            log.info("订单修改前：{}", orderInfo);
            orderInfo.setOrderStatus(OrderStatus.TOBEPAID);
            log.info("订单修改后：{}", orderInfo);
        } else {
            throw new RuntimeException("订单-" + orderNo + "-状态不为确认中");
        }
        updateById(orderInfo);

        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("order_no", orderNo);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new OrderMainVo(orderInfo, orderDetails);
    }
}
